﻿using UnityEngine;
using System.Collections;

public class UnitNote : MonoBehaviour
{

    public int iIndex;
    public UnitStatus unitStatus;

    int iOriginDepth;

    bool bFolding;
    bool bFoldingTime; // FoldingError exception catch

    bool bUnFoldingInProgress;
    bool bFoldInProgress;

    bool bFoldComplete;

    GameObject Details;
    // Use this for initialization
    void Start()
    {
        Details = Resources.Load("Prefaps/UI/StaticUI/UnitNote/UnitNoteDetails") as GameObject;
        iOriginDepth = GetComponent<UISprite>().depth;

        bFoldingTime = true;
        bFolding = true;
        bUnFoldingInProgress = false;
        bFoldInProgress = false;
        bFoldComplete = false;

        unitStatus = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList[iIndex].transform.GetComponent<UnitStatus>();

        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).GetComponent<UISprite>())
                transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
            else if ((transform.GetChild(i).GetComponent<UILabel>()))
                transform.GetChild(i).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
        }

        string strClassName;

        switch (unitStatus.m_unitData.ClassStatus.enClass)
        {
            case CLASS_TYPE.POLICE:
                strClassName = "Police";
                break;

            case CLASS_TYPE.TECHNICIAN:
                strClassName = "Technician";
                break;

            case CLASS_TYPE.SOLDIER:
                strClassName = "Army";
                break;

            default:
                strClassName = "Error";
                break;
        }
        strClassName += string.Format("{0}", (int)unitStatus.m_unitData.ClassStatus.AssnStatus.enAssnType);

        transform.FindChild("UnitNoteClass").GetComponent<UISprite>().spriteName = strClassName;


        string strRankName;
        strRankName = string.Format("Rank{0}", unitStatus.m_unitData.ClassStatus.AssnStatus.iRank);
        transform.FindChild("UnitNoteRank").GetComponent<UISprite>().spriteName = strRankName;

        transform.FindChild("UnitNoteFace").GetComponent<UISprite>().spriteName = unitStatus.m_unitData.strFaceName;

        NoteNaming();



        string strBackText;
        Color BackColor;

        //Current HP
        if (unitStatus.m_unitData.iCurrentHP == unitStatus.m_unitData.iMaxHP)
        {
            strBackText = "HEALTHY";
            BackColor = new Color(255 / 255f, 0 / 255f, 0 / 255f);
            BackColor = Color.red;
        }
        else if (unitStatus.m_unitData.iCurrentHP >= (unitStatus.m_unitData.iMaxHP * 3f / 4f))
        {
            strBackText = "SCRATCHY";
            BackColor = new Color(219 / 255f, 0 / 255f, 0 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentHP >= (unitStatus.m_unitData.iMaxHP * 1f / 2f))
        {
            strBackText = "WOUNDED";
            BackColor = new Color(183 / 255f, 0 / 255f, 0 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentHP >= (unitStatus.m_unitData.iMaxHP * 1f / 4f))
        {
            strBackText = "CRITICAL";
            BackColor = new Color(147 / 255f, 0 / 255f, 0 / 255f);
        }
        else
        {
            strBackText = "DYING";
            BackColor = new Color(110 / 255f, 0 / 255f, 0 / 255f);
        }

        transform.FindChild("UnitNoteCurHP").GetComponent<UILabel>().text = strBackText;
        transform.FindChild("UnitNoteCurHP").GetComponent<UILabel>().color = BackColor;


        //Current MORALE
        if (unitStatus.m_unitData.iCurrentMorale == unitStatus.m_unitData.iMorale)
        {
            strBackText = "INSPIRED";
            BackColor = new Color(255 / 255f, 255 / 255f, 0 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentMorale >= (unitStatus.m_unitData.iMaxMorale * 3f / 4f))
        {
            strBackText = "ENCOURAGED";
            BackColor = new Color(255 / 255f, 235 / 255f, 0 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentMorale >= (unitStatus.m_unitData.iMaxMorale * 1f / 2f))
        {
            strBackText = "NORMAL";
            BackColor = new Color(255 / 255f, 215 / 255f, 0 / 255f);
        }
        else if (unitStatus.m_unitData.iCurrentMorale >= (unitStatus.m_unitData.iMaxMorale * 1f / 4f))
        {
            strBackText = "DISPIRITED";
            BackColor = new Color(255 / 255f, 195 / 255f, 0 / 255f);
        }
        else
        {
            strBackText = "PANIC";
            BackColor = new Color(255 / 255f, 175 / 255f, 0 / 255f);
        }

        transform.FindChild("UnitNoteCurMorale").GetComponent<UILabel>().text = strBackText;
        transform.FindChild("UnitNoteCurMorale").GetComponent<UILabel>().color = BackColor;


    }

    // Update is called once per frame
    void Update()
    {
        if (bUnFoldingInProgress)
        {
            if (!GetComponent<UISpriteAnimation>().isPlaying)
            { // Unfold Complete!
                GetComponent<UISpriteAnimation>().enabled = false;
                bUnFoldingInProgress = false;
                EventDelegate.Remove(transform.parent.GetComponent<TweenPosition>().onFinished, UnFoldingPaper);
                bFoldingTime = true;
                (Instantiate(Details) as GameObject).transform.parent = transform;
                transform.FindChild("UnitNoteDetails(Clone)").transform.localPosition = Vector3.zero;
                transform.FindChild("UnitNoteDetails(Clone)").transform.localScale = Vector3.one;
                transform.FindChild("UnitNoteDetails(Clone)").Find("UnitNoteWeapon").GetComponent<UISprite>().spriteName = unitStatus.m_WeaponData.m_strWeaponName;
            }
        }

        if (bFoldInProgress)
        {
            if (!GetComponent<UISpriteAnimation>().isPlaying)
            {
                TweenPosToOriginPos();
                SetDepth(false);
            }
        }

        if (!bFolding)
            if (GameObject.Find("NickNameSetPaper") == null)
                if (Input.GetKeyUp(KeyCode.Escape))
                    OnClick();

    }


    public void NoteNaming()
    {
        if (unitStatus.m_unitData.strNickName == "")
            transform.FindChild("UnitNoteNameLabel").GetComponent<UILabel>().text = string.Format("{0} {1}", unitStatus.m_unitData.strFirstName, unitStatus.m_unitData.strLastName);
        else
            transform.FindChild("UnitNoteNameLabel").GetComponent<UILabel>().text = string.Format("{0} '{1}' {2}", unitStatus.m_unitData.strFirstName, unitStatus.m_unitData.strNickName, unitStatus.m_unitData.strLastName);


    }

    void LateUpdate()
    {
        if (bFoldComplete)
        {
            EventDelegate.Remove(transform.parent.GetComponent<TweenPosition>().onFinished, InReachedOrginPos);
            bFoldComplete = false;
        }
    }

    public void OnClick()
    {
        if (bFoldingTime)
        {
            if (bFolding)
            {
                transform.parent.GetComponent<UnitNoteParent>().iSelNote = iIndex;
                transform.parent.GetComponent<TweenPosition>().from = transform.parent.transform.localPosition;
                transform.parent.GetComponent<TweenPosition>().to = -transform.localPosition;
                transform.parent.GetComponent<TweenPosition>().Play(true);
                EventDelegate.Add(transform.parent.GetComponent<TweenPosition>().onFinished, UnFoldingPaper);
            }
            else
            {
                FoldingPaper();
            }
            bFoldingTime = false;
        }
    }

    public void FoldingPaper()
    {
        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Paper_Fold_1"));

        GetComponent<UISpriteAnimation>().Reset(7);
        GetComponent<UISpriteAnimation>().Reverse = true;
        GetComponent<UISpriteAnimation>().enabled = true;
        bFoldInProgress = true;
        bFolding = true;
        Destroy(transform.FindChild("UnitNoteDetails(Clone)").gameObject);
    }

    public void UnFoldingPaper()
    {
        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Paper_Fold_0"));

        GetComponent<UISpriteAnimation>().Reset(0);
        GetComponent<UISpriteAnimation>().Reverse = false;
        GetComponent<UISpriteAnimation>().enabled = true;
        SetDepth(true);
        bUnFoldingInProgress = true;
        bFolding = false;
    }

    public void TweenPosToOriginPos()
    {
        transform.parent.GetComponent<TweenPosition>().Play(false);
        EventDelegate.Add(transform.parent.GetComponent<TweenPosition>().onFinished, InReachedOrginPos);
    }

    public void InReachedOrginPos()
    { // Fold Complete!
        transform.parent.GetComponent<UnitNoteParent>().iSelNote = 99;
        bFoldInProgress = false;
        bFoldingTime = true;
        bFoldComplete = true;
    }

    public void DisableRect()
    {
        GetComponent<BoxCollider>().size = Vector3.zero;
    }

    public void EnableRect()
    {
        GetComponent<BoxCollider>().size = new Vector3(100, 14);
    }

    void SetDepth(bool bPlus)
    {
        if (bPlus)
        {
            GetComponent<UISprite>().depth = iOriginDepth + 2;
            for (int i = 0; i < transform.childCount; ++i)
            {
                if (transform.GetChild(i).GetComponent<UISprite>())
                    transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
                else if (transform.GetChild(i).GetComponent<UILabel>())
                    transform.GetChild(i).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
            }
        }
        else
        {
            GetComponent<UISprite>().depth = iOriginDepth;
            for (int i = 0; i < transform.childCount; ++i)
            {
                if (transform.GetChild(i).GetComponent<UISprite>())
                    transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
                else if (transform.GetChild(i).GetComponent<UILabel>())
                    transform.GetChild(i).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
            }
        }
    }

    public void DestroyThis()
    {
        Destroy(gameObject);
    }
}
